package weibo;

import user.UserSession;
import utils.JsonOut;
import utils.MysqlUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

@WebServlet(name = "WeiboDetailServlet")
public class WeiboDetailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        String blogId = request.getParameter("blog_id");
        if (blogId == null || blogId.isEmpty()){
            out.println(JsonOut.error(201, "no_blog_id"));
            return;
        }

        ResultSet weiboSet = (new MysqlUtils()).getWeiboDetail(blogId);

        if (weiboSet == null){
            out.println(JsonOut.error(101, "not_exist"));
            return;
        }

        HashMap<String, Object> item = new HashMap<>();
        try{
            // blog.id,blog.user_id,nickname,head_image,blog.content,blog.time,like_num,comment_num,reblog_user,reblog_content,reblog_time
            //select blog.id,blog.user_id,user.nickname,user.head_image,blog.content,blog.time,blog.reblog_id\n" +
            //"b2.user_id as reblog_user_id,u2.nickname as reblog_nickname,u2.head_image as reblog_headimg,b2.content as reblog_content,b2.time as reblog_time\n" +

            String thisBlogId = weiboSet.getString(1);
            String userId = weiboSet.getString(2);
            String nickname = weiboSet.getString(3);
            String headImage = weiboSet.getString(4);
            String content = weiboSet.getString(5);
            String time = weiboSet.getString(6);
//            String likeNum = weiboSet.getString(7);
//            String commentNum = weiboSet.getString(8);
            String reblogId = weiboSet.getString(7);
            String reblogUserId = weiboSet.getString(8);
            String reblogNickname = weiboSet.getString(9);
            String reblogHead = weiboSet.getString(10);
            String reblogContent = weiboSet.getString(11);
            String reblogTime = weiboSet.getString(12);
            item.put("blog_id", thisBlogId != null ? thisBlogId : "");
            item.put("user_id", userId != null ? userId : "");
            item.put("nickname", nickname != null ? nickname : "");
            item.put("head_image", headImage != null ? headImage : "");
            item.put("content", content != null ? content : "");
            item.put("time", time != null ? time : "");

//            item.put("comment_num", commentNum != null ? commentNum : "");
            item.put("reblog_id", reblogId != null ? reblogId : "");
            item.put("reblog_user_id", reblogUserId != null ? reblogUserId : "");
            item.put("reblog_nickname", reblogNickname != null ? reblogNickname : "");
            item.put("reblog_head_image", reblogHead != null ? reblogHead : "");
            item.put("reblog_content", reblogContent != null ? reblogContent : "");
            item.put("reblog_time", reblogTime != null ? reblogTime : "");

            String image = weiboSet.getString(13);
            String reblogImage = weiboSet.getString(14);
            item.put("image", image != null ? image : "");
            item.put("reblog_image", reblogImage != null ? reblogImage : "");

//            int reblogNum = (new MysqlUtils()).getWeiboReblogNum(thisBlogId);
//            item.put("reblog_num", String.valueOf(reblogNum));

            String likeNum = (new MysqlUtils()).weiboLikeNum(thisBlogId);
            item.put("like_num", likeNum != null ? likeNum : "");

            HttpSession session = request.getSession();

            ArrayList<HashMap<String, String>> array = Weibo.getComment(blogId, 0, 10, session);
            if (array != null) item.put("comment_list", array);

            String email = UserSession.getUserSessionEmail(session);
            if (email != null && !email.isEmpty()){
                // can_delete判断是否能删除
                item.put("can_delete", (new MysqlUtils()).canDeleteWeibo(email, thisBlogId != null ? thisBlogId : "") ? "1" : "0");
                // liked判断是否赞了
                item.put("liked", (new MysqlUtils()).isLikeWeibo(email, thisBlogId != null ? thisBlogId : "") ? "1" : "0");
            }



            out.println(JsonOut.common(0, "weibo_detail", item));
        }catch (Exception e){
            e.printStackTrace();
            out.println(JsonOut.error(500, "sql_sth_wrong"));
        }


    }
}
